"""
@Author：十
@Time：2024/6/21 12:08
@FileName：ConfigWindow.py
@Description：配置窗口
"""
import sys

from PyQt5 import QtCore
from PyQt5.QtCore import pyqtSignal, Qt, QRect, QSize
from PyQt5.QtGui import QPainter, QFontMetrics, QPalette, QFont
from PyQt5.QtWidgets import QWidget, QApplication, QTabBar, QColorDialog

from ConfigUI import Ui_Form


class HorizontalTabBar(QTabBar):
    def paintEvent(self, event):
        painter = QPainter(self)
        option = self.tabRect(0)

        for index in range(self.count()):
            tab_rect = self.tabRect(index)
            tab_text = self.tabText(index)

            painter.save()
            painter.translate(tab_rect.topLeft())
            painter.rotate(0)  # 保持水平，不旋转
            painter.drawText(QRect(0, 0, tab_rect.width(), tab_rect.height()),
                             Qt.AlignCenter, tab_text)
            painter.restore()

        painter.end()

    def tabSizeHint(self, index):
        size = super().tabSizeHint(index)
        fm = QFontMetrics(self.font())
        size.transpose()  # 交换宽度和高度
        return QSize(size.width(), fm.height() + 10)  # 增加一些额外的高度


class ConfigWindow(QWidget, Ui_Form):
    Signal_Data_Tristate = pyqtSignal(object)

    def __init__(self, parent=None):
        super(ConfigWindow, self).__init__()
        self.setupUi(self)

        self.parent = parent

        self.pushButton.clicked.connect(self.PushConfig)

        # 界面初始化
        self.Interface_Init()

        # 透明度Slider变化
        self.horizontalSlider_Transparency.valueChanged.connect(self.Slider_Transparency)

        # 显示模式改变
        self.radioButton_Display_Windows.toggled.connect(self.Display_Mode)
        self.radioButton_Display_Desktop.toggled.connect(self.Display_Mode)
        self.radioButton_Display_Top.toggled.connect(self.Display_Mode)

        # 隐藏主界面文字
        self.radioButton_Hidden_Text.toggled.connect(self.Hide_Text)

        # 字体选择
        # self.fontComboBox.currentIndexChanged.connect(self.Font_Style)

        # 字体大小选择
        self.spinBox_Font_Size.valueChanged.connect(self.Font_Size)

        # 字体颜色选择器
        self.pushButton_Font_Color.clicked.connect(self.Font_Color)

    # 获取配置
    def PushConfig(self):
        Work_Start = self.timeEdit_Work_Start.time()
        Work_End = self.timeEdit_Work_End.time()
        Siesta_Start = self.timeEdit_Siesta_Start.time()
        Siesta_End = self.timeEdit_Siesta_End.time()
        Salary = self.lineEdit_Salary.text()
        Vacation = self.comboBox_Vacation.currentIndex()
        config = {'Work_Start': Work_Start, 'Work_End': Work_End, 'Siesta_Start': Siesta_Start, 'Siesta_End': Siesta_End, 'Salary': Salary, 'Vacation': Vacation}
        self.Signal_Data_Tristate.emit(config)
        # print(config)

    # 获取透明度Slider
    def Slider_Transparency(self):
        try:
            transparency = self.horizontalSlider_Transparency.value()
            self.label_Transparency.setText(str(transparency) + '%')
            transparency = 255 / 100 * transparency
            transparency = 'background-color: rgba(255, 255, 255,' + str(transparency) + ');'
            self.parent.widget.setStyleSheet(transparency)
            # self.parent.Form.setStyleSheet(transparency)
        except Exception as e:
            print(e)

    # 显示模式选择
    def Display_Mode(self):
        try:
            if self.radioButton_Display_Windows.isChecked():
                # 窗口
                # 取消置顶
                self.parent.windowHandle().setFlags(self.parent.windowFlags())
                # 取消固定在桌面

            elif self.radioButton_Display_Desktop.isChecked():
                # 固定在桌面
                # self.parent.Signal_Embed_to_Desktop.emit()
                pass

            elif self.radioButton_Display_Top.isChecked():
                # 置顶
                try:
                    # 设置为置顶
                    m_flags = self.parent.windowFlags()
                    self.parent.windowHandle().setFlags(m_flags | Qt.WindowType.WindowStaysOnTopHint)
                    # 取消固定在桌面
                except Exception as e:
                    print(e)
        except Exception as e:
            print('Change Display Mode ERROR:', e)

    # 隐藏文字
    def Hide_Text(self):
        try:
            if self.radioButton_Hidden_Text.isChecked():
                self.parent.widget_2.hide()
                self.parent.label_Gains.show()
                # self.parent.pushButton_Config.hide()
            else:
                self.parent.label.show()
                self.parent.widget_2.show()

                # self.parent.pushButton_Config.show()
        except Exception as e:
            print('Hide_Text error: ', e)

    # 字体选择
    # def Font_Style(self):
    #     try:
    #         font = self.fontComboBox.currentFont()
    #         print('Font Style:', font)
    #         font_size = int(self.parent.label_Gains.font().pointSize())
    #         print('Font Size:', font_size)
    #         self.parent.label_Gains.setFont(QFont(font))
    #     except Exception as e:
    #         print('Set Font ERROR:', e)

    # 字体大小选择
    def Font_Size(self):
        try:
            font_size = self.spinBox_Font_Size.value()
            font = self.parent.label_Gains.font().lastResortFont()
            self.parent.label_Gains.setFont(QFont(font, font_size))
        except Exception as e:
            print('Size Choose Error:', e)

    # 字体颜色选择器
    def Font_Color(self):
        try:
            col = QColorDialog.getColor()
            pal = self.parent.label_Gains.palette()
            pal.setColor(QPalette.WindowText, col)
            self.parent.label_Gains.setPalette(pal)
        except Exception as e:
            print('Color Choose Error:', e)

    def Interface_Init(self):
        # 没有任务栏
        self.setWindowFlag(QtCore.Qt.Tool)

        # self.setAttribute(Qt.WA_TranslucentBackground)

        # 透明度Slider初始位
        self.horizontalSlider_Transparency.setMinimum(0)
        self.horizontalSlider_Transparency.setMaximum(100)
        self.horizontalSlider_Transparency.setValue(100)
        self.Slider_Transparency()

        # 当前字体大小
        self.spinBox_Font_Size.setValue(self.parent.label_Gains.font().pointSize())

        # TODO：tab在左且文字水平
        self.tabWidget.tabText(Qt.AlignVCenter | Qt.AlignRight)
        # self.tabWidget.setTabBar(HorizontalTabBar())
        # self.tabWidget.


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ConfigWindow()
    window.show()
    sys.exit(app.exec_())
